﻿using System;
using System.Reflection;
using Xunit;

namespace ReflectTest
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {

            Type t = Assembly.GetExecutingAssembly().GetType("ReflectAndMetadata.TypeSample");
            Console.WriteLine(t);
        }

        [Fact]
        public void TestTypeInfoData()
        {
            Type stringType = typeof(string);
            string name = stringType.Name;          // String
            Type baseType = stringType.GetTypeInfo().BaseType;      // typeof(Object)
            Assembly assem = stringType.Assembly;      // System.Private.CoreLib
            bool isPublic = stringType.IsPublic;      // true

            Assert.Equal("String", name);
            Assert.Equal(typeof(Object), baseType);
            Assert.Equal("System.Private.CoreLib", assem.GetName().Name);
            Assert.True(isPublic);

        }

        [Fact]
        public void TestIsEquivalent()
        {
            object obj = Guid.NewGuid();
            Type target = typeof(IFormattable);

            bool isTrue = obj is IFormattable;             // Static C# operator
            bool alsoTrue = target.IsInstanceOfType(obj);   // Dynamic equivalent

            Assert.True(isTrue, "is");
            Assert.True(alsoTrue, "is 等效");
        }
        [Fact]
        public void TestIsAssignableFrom()
        {
            Type target = typeof(IComparable), source = typeof(string);
            Console.WriteLine(target.IsAssignableFrom(source));
        }
    }
}
